//
//  UIFont+LYAdd.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/8/14.
//

#import "UIFont+LYAdd.h"
#import "LYUIMacros.h"
#import <CoreText/CTFontManager.h>

@interface UIFont_LYAdd : NSObject

@end
@implementation UIFont_LYAdd

@end

static NSString *kIcontFontFileName = @"iconfont";
@implementation UIFont (LYAdd)

+ (void)registerIconFontWithName:(NSString *)name
{
    NSAssert(name.length, @"font name is invalid");
    
    kIcontFontFileName = name.copy;
    
    UIFont *font = [UIFont fontWithName:name size:16];
    if (font)
    {
        /// 已经注册过
        return;
    }
    
    NSURL *fontFileUrl = [[NSBundle mainBundle] URLForResource:name withExtension:@"ttf"];
    
    [self registerIconFontWithFileUrl:fontFileUrl];
}

+ (void)registerIconFontWithFileUrl:(NSURL *)fileUrl
{
    NSAssert([[NSFileManager defaultManager] fileExistsAtPath:[fileUrl path]], @"Font file doesn't exist");
    
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fileUrl);
    
    CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
    
    CGDataProviderRelease(fontDataProvider);
    
    CTFontManagerRegisterGraphicsFont(newFont, nil);
    
    CGFontRelease(newFont);
}

+ (void)setDefaultIconfontFileName:(NSString *)name
{
    if (!name.length)
    {
        return;
    }
    kIcontFontFileName = name.copy;
}

+ (UIFont *)iconfontWithSize:(CGFloat)size
{
    return [UIFont fontWithName:kIcontFontFileName size:size];
}

@end

@implementation NSString (IconFont)

- (NSInteger)hexValue
{
    NSInteger length = self.length;
    NSInteger hexValue = 0;
    
    for (NSInteger i = 0; i < length; i++)
    {
        NSInteger a = [self substringWithRange:(NSRange){i, 1}].integerValue;
        hexValue += a * powf(16, (length - 1 - i));
    }
    
    return hexValue;
}

- (NSString *)iconUnicode
{
    unsigned short count = 0x0000e000;
    count += self.hexValue;
    return [[NSString alloc]initWithCharacters:&count length:1];
}
@end



@implementation UIImage (IconFont)

static UIColor *kIconFontTintColor = nil;

+ (void)setIconFontImageAppearanceColor:(UIColor *)color
{
    kIconFontTintColor = color;
}

+ (UIImage *)iconWithFont:(UIFont *)font
                  unicode:(NSString *)text
                    color:(UIColor *)color
              orientation:(UIImageOrientation)orientation
{
    if (!font)
    {
        return nil;
    }
    CGFloat size = font.pointSize;
    
    UIGraphicsBeginImageContext(CGSizeMake(size, size));
    
    color = color ?: kIconFontTintColor;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if ([text respondsToSelector:@selector(drawAtPoint:withAttributes:)])
    {
        
        /**
         
         * 如果这里抛出异常，请打开断点列表，右击All Exceptions -> Edit Breakpoint -> All修改为Objective-C
         
         * See: http://stackoverflow.com/questions/1163981/how-to-add-a-breakpoint-to-objc-exception-throw/14767076#14767076
         
         */
        
        [text drawAtPoint:CGPointZero withAttributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName:color}];
        
    }
    else
    {
        
#pragma clang diagnostic push
        
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        
        CGContextSetFillColorWithColor(context, color.CGColor);
        
        [text drawAtPoint:CGPointMake(0, 0) withFont:font];
        
#pragma clang pop
        
    }
    
    CGFloat scale = [UIScreen mainScreen].scale;
    
    UIImage *image = [UIImage imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:scale orientation:orientation];
    
    UIGraphicsEndImageContext();
    
    return image;
    
}

+ (UIImage *)iconWithFontSize:(CGFloat)fontSize
                  unicode:(NSString *)text
                    color:(UIColor *)color
              orientation:(UIImageOrientation)orientation
{
    CGFloat scale = [UIScreen mainScreen].scale;
    UIFont *font = [UIFont iconfontWithSize:fontSize * scale];
    
    return [self iconWithFont:font
                      unicode:text
                        color:color
                  orientation:orientation];
}

+ (UIImage *)iconWithFontSize:(CGFloat)fontSize
                  unicode:(NSString *)text
                    color:(UIColor *)color
{
    return [self iconWithFontSize:fontSize
                      unicode:text
                        color:color
                  orientation:0];
}

+ (UIImage *)iconWithFontSize:(CGFloat)fontSize
                  unicode:(NSString *)text
              orientation:(UIImageOrientation)orientation
{
    return [self iconWithFontSize:fontSize
                      unicode:text
                        color:nil
                  orientation:orientation];
}

+ (UIImage *)iconWithFontSize:(CGFloat)fontSize
                  unicode:(NSString *)text
{
    return [self iconWithFontSize:fontSize
                      unicode:text
                        color:nil
                  orientation:0];
}

@end

